package com.shujia.spark

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo12Sort {


  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[4]").setAppName("map")
    val sc: SparkContext = new SparkContext(conf)


    val scores: RDD[String] = sc.textFile("spark/data/score.txt")


    //统计总分
    val kvRDD: RDD[(String, Int)] = scores.map(line => {
      val id: String = line.split(",")(0)
      val sco: Int = line.split(",")(2).toInt
      (id, sco)
    })

    val sumScoreRDD: RDD[(String, Int)] = kvRDD.reduceByKey(_ + _)


    //按总分降序
    val sortRDD: RDD[(String, Int)] = sumScoreRDD.sortBy(kv => kv._2, ascending = false)


    //去top10

    val top10: Array[(String, Int)] = sortRDD.take(10)


    top10.foreach(println)


  }

}
